import 'dart:async';
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:imember/Util/Global.dart';
import 'package:imember/Util/MyColor.dart';
import 'package:imember/Util/PageUtil.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'Login.dart';

class Splash extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primaryColor: MyColor.White,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      builder: (context, child) {
        return FlutterEasyLoading(child: child);
      },
      home: SplashPage(),
    );
  }
}

class SplashPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new _SplashPage();
  }
}

class _SplashPage extends State<SplashPage> {
  @override
  void initState() {
    openLoginPageByTimer(context);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: MyColor.White,
      alignment: Alignment.bottomCenter
    );
  }

  openLoginPageByTimer(BuildContext context) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    Global.token = prefs.getString("token") ?? "";
    print("token:" + Global.token);
    const timeout = const Duration(seconds: 1);
    Timer(timeout, () {
      PageUtil().openReplace(context, Login());
    });
  }
}
